// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 * Author: Huawei OS Kernel Lab
 * Create: Thu Aug 15 16:14:23 2023
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/suspend.h>
#include <notifier-error-inject.h>

#include "suspend_fail_notifier.h"

static struct notifier_err_inject pm_suspend_notifier_err_inject = {
	.actions = {
		{ NOTIFIER_ERR_INJECT_ACTION(PM_TEST_PLAT_BEGIN) },
		{ NOTIFIER_ERR_INJECT_ACTION(PM_TEST_DPM_START) },
		{ NOTIFIER_ERR_INJECT_ACTION(PM_TEST_PLAT_PRE) },
		{ NOTIFIER_ERR_INJECT_ACTION(PM_TEST_DPM_LATE) },
		{ NOTIFIER_ERR_INJECT_ACTION(PM_TEST_DPM_NOIRQ) },
		{ NOTIFIER_ERR_INJECT_ACTION(PM_TEST_NONBOOT_CPU) },
		{}
	}
};

static struct dentry *dir;

static int pm_suspend_err_inject_init(void)
{
	int err;
	int priority = 0;

	dir = notifier_err_inject_init("pm_suspend", notifier_err_inject_dir,
					&pm_suspend_notifier_err_inject, priority);
	if (IS_ERR(dir))
		return PTR_ERR(dir);

	err = suspend_error_inject_register(&pm_suspend_notifier_err_inject.nb);
	if (err)
		debugfs_remove_recursive(dir);

	return err;
}

static void pm_suspend_err_inject_exit(void)
{
	suspend_error_inject_unregister(&pm_suspend_notifier_err_inject.nb);
	debugfs_remove_recursive(dir);
}

module_init(pm_suspend_err_inject_init);
module_exit(pm_suspend_err_inject_exit);

MODULE_DESCRIPTION("pm suspend notifier error injection module");
MODULE_LICENSE("GPL");
